package com.dwusoft.banks.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//import com.chinaums.xm.macUtil.Base64;
//import com.chinaums.xm.macUtil.DESPlus;
//import com.chinaums.xm.macUtil.MACGenerator;
/*
 *  厦门验密接口--web页面跳转
 */
//import com.chinaums.xm.macUtil.Translation;

@Controller
@RequestMapping("/xmym")
public class XmYanMiController {
	
	private final static Logger logger = LoggerFactory.getLogger(XmYanMiController.class);
	private final static String actionUrl = "http://218.5.69.213:6080/mer/cvMer/cpVerify.sp?sendVf";
	public static SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat sfTime = new SimpleDateFormat("hhmmss");
	
	
	@RequestMapping(value = "{action}",method = RequestMethod.GET)
	public void yanmiget(ModelMap modelMap,HttpServletRequest request){
		logger.info("实时接收返回信息================接收返回报文对象=================");
		return ;
//		String base = request.getContextPath();
//		String fullPath = request.getScheme()+"://" + request.getServerName() +":"+ request.getServerPort() + base;
//		logger.info("访问： "+fullPath);
//		modelMap.addAttribute("base", base);
	}
	
	
	@RequestMapping(value = "{actionURL}",method = RequestMethod.POST)
	public String yanmipost(ModelMap model,HttpServletRequest request){
		
		String mer_key   = request.getParameter("mer_key");//商户密钥           
		String merId = request.getParameter("merId");//商户ID               
		String transNo   = request.getParameter("transNo");//交易流水号             
		String inputDate = sfDate.format(new Date());//操作日期               
		String inputTime = sfTime.format(new Date());//操作时间                     
//		String realName  = Translation.toUnicode(request.getParameter("realName"));//姓名                   
		String certType  = request.getParameter("certType");//证件类型               
		String certNo    = request.getParameter("certNo");//证件号码               
		String cardNo    = request.getParameter("cardNo");//卡号                   
		String pageRetUrl = request.getParameter("pageRetUrl");;//交易完成页面跳转URL    
		String bgRetUrl   = request.getParameter("bgRetUrl");//交易结果通知后台接收URL
		String private1  = "";//私有域                 
		String chkValue  = "";//签名值          
		
//		DESPlus des = new DESPlus(mer_key);
//		certNo = des.encrypt(certNo);
//		cardNo = des.encrypt(cardNo);

//		String reqStr = transNo + merId + inputDate + inputTime + realName + certType + certNo + cardNo + pageRetUrl + bgRetUrl + private1;
		String reqStr = transNo + merId + inputDate + inputTime + certType + certNo + cardNo + pageRetUrl + bgRetUrl + private1;
		//String reqStr = transNo + merId + inputDate + inputTime + realName + certNo + cardNo;
		logger.info("异步接收返回信息================接收返回报文对象=================");
		System.out.println("reqStr===="+reqStr);
//		String req_mac = MACGenerator.getANSIX99MAC(reqStr, mer_key);
//		chkValue =  new String(Base64.encode(req_mac.getBytes()));
		
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("merId", merId);
		paraMap.put("transNo", transNo);
		paraMap.put("inputDate", inputDate);
		paraMap.put("inputTime", inputTime);
//		paraMap.put("realName", realName);
		paraMap.put("certType", certType);
		paraMap.put("certNo", certNo);
		paraMap.put("cardNo", cardNo);
		paraMap.put("pageRetUrl", pageRetUrl);
		paraMap.put("bgRetUrl", bgRetUrl);
		paraMap.put("private1", private1);
		paraMap.put("chkValue", chkValue);
		
		model.put("actionUrl", actionUrl);
		model.put("paraMap", paraMap);
		
//		return "redirect2";
		return null;
	}
	
}
